package com.meixiang.activity.account.login;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.tool.Tool;
import com.meixiang.util.AbToastUtil;
import com.meixiang.util.DesUtil;
import com.meixiang.view.ClearEditText;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册
 *
 * @author zys
 */

public class RegisterActivity extends BaseActivity {
    /**
     * 手机号
     */
    @Bind(R.id.cet_input_name)
    ClearEditText mCetInputName;
    /**
     * 验证码按钮
     */
    @Bind(R.id.tv_verification)
    TextView mTvVerification;
    /**
     * 密码
     */
    @Bind(R.id.cet_input_pwd)
    ClearEditText mCetInputPwd;
    /**
     * 验证码
     */
    @Bind(R.id.cet_verification_code)
    ClearEditText mCetVerificationCode;
    /**
     * 确定
     */
    @Bind(R.id.tv_register)
    TextView mTvRegister;

    private Activity mActivity;


    //短信验证码
    CountDownTimer regetTimer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            mTvVerification.setText((millisUntilFinished / 1000) + "(S)");
            mTvVerification.setEnabled(false);
        }

        @Override
        public void onFinish() {
            mTvVerification.setText("验证");
            mTvVerification.setEnabled(true);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        mActivity = RegisterActivity.this;
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        setTitle(getResources().getString(R.string.register));


    }

    @Override
    protected void initView() {
    }

    @Override
    protected void setData() {

    }

//    /**
//     * 验证手机号或者邮箱以及验证码是否可注册
//     */
//    private void sendCanSign() {
//        Log.e("TAG","输出当前的手机号=="+mCetInputName.getText().toString());
//        HttpParams params = new HttpParams();
//        params.put("mobile", mCetInputName.getText().toString());
//        HttpUtils.post(Config.CANSIGN , params, new HttpCallBack(mActivity) {
//            @Override
//            public void onSucceed(JSONObject result, String code, String message) {
//                if ("0".equals(code)) {
//                    sendVerificationCode();
//                    regetTimer.start();
//                    mTvVerification.setEnabled(false);
//                } else
//                    AbToastUtil.showToast(mActivity, message);
//            }
//
//            @Override
//            public void onError(String code, String message) {
//                AbToastUtil.showToast(mActivity, message);
//            }
//        });
//    }

    /**
     * 短信验证
     */
    private void sendVerificationCode() {
        HttpParams params = new HttpParams();
        params.put("mobile", mCetInputName.getText().toString());
        HttpUtils.post(Config.REGISTER_SEND_CODE, params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                regetTimer.start();
                AbToastUtil.showToast(mActivity, message);
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(mActivity, message);
            }
        });
    }

    @OnClick({R.id.tv_verification, R.id.tv_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_verification:
                if ((mCetInputName.getText().toString().length()) != 11) {
                    Tool.showTextToast(mActivity, "请输入11位手机号码");
                    return;
                }
                sendVerificationCode();
                break;
            case R.id.tv_register:

                if (mCetInputPwd.getText().toString().trim().length() < 6 || mCetInputPwd.getText().toString().trim().length() > 16) {
                    Tool.showTextToast(mActivity, "密码不能小于6~16位");
                    return;
                }
                if (TextUtils.isEmpty(mCetVerificationCode.getText().toString())) {
                    Tool.showTextToast(mActivity, "请输入验证码");
                    return;
                }
                getSign();
                break;
        }
    }


    /**
     * 注册
     */
    private void getSign() {
        HttpParams params = new HttpParams();
        params.put("password", DesUtil.md5(mCetInputPwd.getText().toString().trim()));
        params.put("tab", "0");//	0:手机注册 ，1:邮箱注册
        params.put("mobile", mCetInputName.getText().toString());
        params.put("mobileCode", mCetVerificationCode.getText().toString());
        HttpUtils.post(Config.REGISTER, params, new HttpCallBack(mActivity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
//                startActivity(new Intent(mActivity, RegisterSucceedActivity.class));
                AbToastUtil.showToast(mActivity, message);
                finish();
            }

            @Override
            public void onError(String code, String message) {
                AbToastUtil.showToast(mActivity, message);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
        regetTimer.cancel();
    }
}
